<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/8
 * Time: 10:17
 */

namespace Admin\Controller;


use Common\Controller\AdminController;
use Think\Page;

class AdminInfoController extends AdminController
{
    /*
     * 管理员列表
     * */
    public function index()
    {
        // 访问这个文章列表有权限设置  $_SESSION['admin']['admin_id'];
        $keyword = I('get.keyword','','htmlspecialchars');
        $search = I('get.search');
        $Admin = M('AdminInfoView');
        if (!empty($search)) {
            $where['role_name']  = array('eq',$search); //用户角色名称
        }

        if (!empty($keyword)) {
            
            $where['admin_id|real_name']  = array('like', "%$keyword%");
        }

        $dataCount = $Admin->where($where)->count();

        $page = new Page($dataCount, 10);
        $show = $page->show();

        $data = $Admin->where($where)->limit($page->firstRow, $page->listRows)->select();

        $this->assign('data',$data);
        $this->assign('show',$show);

        layout(true);
        $this->display('index');
    }

    /*
     * 编辑用户
     * */
    public function modAdmin($id)
    {
        $id = intval($id);
        $AdminInfo =D('AdminInfo');
        $data = $AdminInfo->where(array(
            'id' => $id
        ))->find();

        if (!$data) {
            $this->redirect('/AdminInfo/index');
        }

        if (IS_POST) {
            if ($AdminInfo->create()) {
                $AdminInfo->admin_pwd = md5($AdminInfo->admin_pwd);
                $AdminInfo->save();
                $this->success('修改成功', '/AdminInfo/index');
                return;
            } else {
                // 自动验证没有通过 输出错误提示信息
                $errData = $AdminInfo->getError();
                $this->assign('errData',$errData);
            }
        }
        $this->assign('data',$data);
        layout(true);
        $this->display('modmanager');
    }

    /*
     * 删除用户
     * */
    public function delAdmin($id)
    {
        $id = intval($id);

        $AdminInfo = M('AdminInfo');
        $findData = $AdminInfo->where(array(
            'id' => $id
        ))->find();

        if ($findData) {
            $AdminInfo->delete($id);
            $this->success('删除成功', '/AdminInfo/index');
        } else {
            $this->error('删除失败', '/AdminInfo/index');
        }
    }

    /*
     * 添加用户
     * */
    public function addAdmin()
    {
        $AdminInfo = D('AdminInfo'); //D参数：模型名 默认当前模块下的Model
        if (IS_POST) {
            $array=I('post.');
            $array['creat_time']=date('Y-m-d H:i:s');
            if ($AdminInfo->create()) {
                //密码加密，添加注册时间
                $array['admin_pwd'] = md5($AdminInfo->admin_pwd);
                $AdminInfo->add($array);
                //添加成功
                $this->success('添加成功', '/AdminInfo/index');
                return;
            } else {
                // 自动验证没有通过 输出错误提示信息
                $errData = $AdminInfo->getError();
                $this->assign('errData',$errData);
            }
        }
        layout(true);
        $this->display('addmanager');
    }
}
